const utils = require('./utils')
const {
  navigateTo
} = require('../../utils/route')

module.exports = Behavior({
  behaviors: [],
  properties: {
    active:{
      type:Boolean,
      value:false
    }
  },
  data: {
    loadMoreSettingStatus: 'more', // [more | loading | noMore]
    list: [],
    isEmpty: null,
    loadmoreShow: false,
  },
  created: function () {},
  attached: function () {
    // 设置缓存全部数据
    this.wholeList = []
    // 设置当前渲染第几页
    this.currentRenderIndex = 0
    // 设置缓存每一页页面高度
    this.pageHeightArr = []
    // 设置总页数
    this.totalPageNum = 0
    // 设置分页
    this.param = {
      limit: 10,
      page: 0
    }

    // this.init()
  },
  ready: function () {},
  moved: function () {},
  detached: function () {},
  observers: {
    'active':function(val){
      // console.log('active',val)
      if(val === true && this.data.list.length === 0){
        this.init();
      }
    },
    'list[0]': function (newVal) {
      // console.log('observers list')
      if (!Array.isArray(newVal)) return;
      if (newVal.length === 0) {
        this.setData({
          loadmoreShow: false
        })
        return;
      }
      wx.nextTick(() => {
        // 在当前同步流程结束后，下一个时间片执行
        const query = this.createSelectorQuery();
        query.select('.custom-scroller-wrapper').boundingClientRect()
        query.select('.list-wrapper').boundingClientRect()
        query.exec((res) => {
          // console.log(res);
          const [custom_scroller_wrapper, list_wrapper] = res
          if (list_wrapper?.height >= custom_scroller_wrapper?.height) {
            this.setData({
              loadmoreShow: true
            })
          }
        })
      })
    }
  },
  methods: {
    ...utils,
    async init(){
      this.refresh()
    },
    async getList(type) {
      const that = this
      const {
        loadMoreSettingStatus
      } = that.data
      // 判断当前是否为加载状态 防止页面重复添加数据
      if (loadMoreSettingStatus !== 'loading') {
        if (type === 'loadmore') {
          that.setData({
            loadMoreSettingStatus: 'loading'
          })
        }
        const page = that.param.page
        that.currentRenderIndex = page
        if (that.totalPageNum > 0 && page == that.totalPageNum) {
          that.setData({
            loadMoreSettingStatus: 'noMore'
          })
        } else {
          this.setData({
            isEmpty:false
          })
          await that.fetchList?.(type);
          //  获取远程数据可换成自己封装的请求方法
          // wx.request({
          //   url: 'https://coolui.coolwl.cn',
          //   data: {
          //     page: page + 1,
          //     isempty: 0, // 设置为1可测试空数据
          //     limit: 10,
          //     pagenum: 10,
          //     islong: 1
          //   },
          //   method: 'get',
          //   success(res) {
          //     if (res.data.code === 200) {
          //       that.totalPageNum = res.data.data.last
          //       if (res.data.data.list.length === 0 && page === 0) {
          //         that.setData({
          //           isEmpty: true,
          //           loadMoreSettingStatus:'noMore'
          //         })
          //       } else {
          //         that.wholeList[page] = res.data.data.list
          //         const datas = {}
          //         datas['list[' + page + ']'] = res.data.data.list
          //         that.setData(datas, () => {
          //           this.setHeight(that)
          //           that.setData({
          //             loadMoreSettingStatus:'more'
          //           })
          //           that.param.page += 1
          //         })
          //       }
          //     }
          //   }
          // })
        }
      }
    },
    async refresh() {
      // 初始化缓存数据
      const that = this
      that.wholeList = []
      that.currentRenderIndex = 0
      that.pageHeightArr = []
      that.param = {
        limit: 10,
        page: 0
      }
      that.setData({
        // list: [],
        loadmoreShow: false,
      })
      // 重新拉取数据
      try {
        await that.getList('refresh')
      } catch (e) {
        console.log('getList-error:',e)
      }
      that.selectComponent('#custom-scroller')?.settriggered(false);
    },
    scrollerRefresh() {
      this.refresh();
    },
    scrollerLoadmore() {
      this.getList('loadmore');
    },
  }
})